package router

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

const successCode = 200
const failCode = 500

type Response struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func ResponseResult(c *gin.Context, err *error, data interface{}) {
	if *err != nil {
		Fail(c, (*err).Error())
		return
	}
	Success(c, data)
}

func newResponse(code int, msg string, data interface{}) Response {
	return Response{
		Code: code,
		Msg:  msg,
		Data: data,
	}
}

func Success(c *gin.Context, data interface{}) {
	resp := newResponse(successCode, "操作成功！", data)
	c.AbortWithStatusJSON(http.StatusOK, resp)
}

func Fail(c *gin.Context, err string) {
	resp := newResponse(failCode, err, "")
	c.AbortWithStatusJSON(http.StatusOK, resp)
}

func Failure(c *gin.Context, code int, err string) {
	resp := newResponse(code, err, "")
	c.AbortWithStatusJSON(http.StatusOK, resp)
}
